package yxy.game.pm2.active.command.barrierfund;

import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.actfundreward;
import yxy.cherry.data.bean.actfundrewarddate;
import yxy.cherry.data.bean.task;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_关卡基金;
import yxy.game.pm2.active.module.DrawStatus;
import yxy.game.pm2.active.module.actdata.ActDataCenter;
import yxy.game.pm2.active.module.subordinateclass.barrierfund.BarrierFund;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.task.Task;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ActiveMessage;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.module.CurrencyHandle;

import java.util.Collection;
import java.util.Map;

@MessageType(proto = Command.ActBarrierFundInfo, classtype = ActiveMessage.ActBarrierFundInfoRequest.class)
public class CMD_Active_关卡基金_Info extends Command<ActiveMessage.ActBarrierFundInfoRequest> {

	@Override
	public void execute(MessageSource messageSource, ActiveMessage.ActBarrierFundInfoRequest request) throws Exception {
		String pcode = messageSource.getTag();
		int type = request.getType();
		ActiveMessage.ActBarrierFundInfoResponse.Builder response = ActiveMessage.ActBarrierFundInfoResponse.newBuilder();

		ActiveModule_关卡基金 am = ActiveModule_关卡基金.get(pcode,false);
		if (am == null) {
			send(messageSource, response, Result.DataError);
			return;
		}

		BarrierFund bf = am.getbfbytype(type);
		if (bf == null) {
			if (type != ActiveModule_关卡基金.acttype_修炼塔基金 && type != ActiveModule_关卡基金.acttype_征战基金	&& type != ActiveModule_关卡基金.acttype_扫荡基金) {
				send(messageSource, response, Result.DataError);
				return;
			}
		}

		boolean buy = bf.getBuytime() == null ? false : true;
		response.setType(type);
		response.setBuy(buy);

		Map<String, actfundreward> afrmap = ActDataCenter.getAllData(actfundreward.class, pcode);
		
		for (Integer id : bf.getIdcodemap().keySet()) {
			String code = bf.getIdcodemap().get(id);
			Task taskbean = Task.get(pcode, code);
			if (taskbean == null) {
				continue;
			}

			actfundreward afr = afrmap.get(String.valueOf(id));
			if(afr == null) {
				continue;
			}
			int taskid = afr.getTaskid();
			task taskexecl = DataCenter.getData(taskid, task.class);
			if (taskexecl == null) {
				continue;
			}

			ActiveMessage.BarrierFundTaskInfo.Builder build = ActiveMessage.BarrierFundTaskInfo.newBuilder();
			build.setId(id);
			
			Collection<Currency> curs = Currency.create(afr.getRewardtype1(), afr.getRewardid1(), afr.getRewardnum1());
			Collection<PackageMessage.Item> items = new CurrencyHandle().packItems(curs);
			build.addAllCurs(items);
			
//			int canget = bf.getDrawids().contains(id) ? ActiveModule_关卡基金.hadget: taskbean.isFinish() ? ActiveModule_关卡基金.canget : ActiveModule_关卡基金.cannotget;
			int canget = bf.getDrawids().contains(id) ? DrawStatus.hadget.getStatus(): taskbean.isFinish() ? DrawStatus.canget.getStatus() : DrawStatus.cannotget.getStatus();
			
			build.setCanget(canget);
			build.setProgress(taskbean.getProgress());

			Collection<Currency> paycurs = Currency.create(afr.getRewardtype2(), afr.getRewardid2(), afr.getRewardnum2());
			Collection<PackageMessage.Item> payitems = new CurrencyHandle().packItems(paycurs);
			build.addAllPaycurs(payitems);
			
			int paycanget = bf.getPaydrawids().contains(id) ? DrawStatus.hadget.getStatus() : taskbean.isFinish() && buy ? DrawStatus.canget.getStatus() : DrawStatus.cannotget.getStatus();
			build.setPaycanget(paycanget);
			
			response.addBftinfos(build.build());

		}
		
		Map<String, actfundrewarddate> afrdmap = ActDataCenter.getAllData(actfundrewarddate.class, pcode); //战令礼包奖励、价格
		for(actfundrewarddate afrd : afrdmap.values()) {
			if(afrd.getGroup() == type) {
				Collection<Currency> curs = Currency.create(afrd.getRewardtype(), afrd.getRewardid(), afrd.getRewardnum());
				Collection<PackageMessage.Item> items = new CurrencyHandle().packItems(curs);
				response.setRebate(afrd.getRebate()); //返利倍数
				response.addAllCurs(items);
			}
		}

		send(messageSource, response, Result.Success);
	}

	private void send(MessageSource messageSource, ActiveMessage.ActBarrierFundInfoResponse.Builder response,
                      Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}

}
